define([
    'jquery',
], function (require, factory) {
    'use strict';
    class CarOperate {
        constructor() {
            this.GLOBAL_STORE = this.queryProduct("carts");
        }
        // getInstance(){
        //     if(!this.instance){
        //         this.instance= new CarOperate();
        //         this.GLOBAL_STORE="a";
        //         console.log(this.GLOBAL_STORE)
        //     }
        //     return this.instance;
        // }
        addProduct(pCat_id, pGoods_id, num, pPrice) {
            var retItem = "";
            if ((this.GLOBAL_STORE[pCat_id] === undefined) || (!this.GLOBAL_STORE[pCat_id] instanceof Array)) {
                this.GLOBAL_STORE[pCat_id] = [];
            }
            var hasGoods = this.GLOBAL_STORE[pCat_id].some((item, index) => {
                if (item.goods_id == pGoods_id) {
                    item.goods_count += num;
                    retItem = item;
                    return true;
                }
            })
            if (!hasGoods) {
                this.GLOBAL_STORE[pCat_id].push({
                    goods_id: pGoods_id,
                    goods_count: num,
                    goods_price: pPrice,
                    goods_isSel: true
                })
                retItem = this.GLOBAL_STORE[pCat_id];
            }
            this.saveData("carts", this.GLOBAL_STORE);
            return { data: this.GLOBAL_STORE, item: retItem };
        }

        reduceProduct(cat_id, goods_id, num) {
            if (!this.GLOBAL_STORE[cat_id]) return false;
            var retItem = "";
            var find = this.GLOBAL_STORE[cat_id].some((item, index) => {
                if (item.goods_id == goods_id) {
                    item.goods_count -= num;
                    retItem = item;
                    return true;
                }
            })
            if (!find) return false;

            this.saveData("carts", this.GLOBAL_STORE);
            return { data: this.GLOBAL_STORE, item: retItem };
        }
        selProduct(cat_id, goods_id, isSel) {
            // console.log(cat_id, goods_id, isSel)
            if (!this.GLOBAL_STORE) {
                this.queryProduct("carts");
            }
            if (!this.GLOBAL_STORE[cat_id]) return false;
            var retItem = "";
            var find = (this.GLOBAL_STORE[cat_id].some((item, index) => {
                // console.log(item.goods_id, goods_id)
                if (item.goods_id == goods_id) {
                    item.goods_isSel = isSel;
                    retItem = item;
                    return true;
                }
            }))
            if (!find) return false;
            this.saveData("carts", this.GLOBAL_STORE);
            return { data: this.GLOBAL_STORE, item: retItem };
        }
        removeProduct(cat_id, goods_id) {
            // console.log(cat_id, goods_id, this.GLOBAL_STORE[cat_id])
            if (!this.GLOBAL_STORE[cat_id]) return false;
            var find = (this.GLOBAL_STORE[cat_id].some((item, index) => {
                if (item.goods_id == goods_id) {
                    // console.log(this.GLOBAL_STORE[cat_id], index)
                    this.GLOBAL_STORE[cat_id].splice(index, 1);
                    if (this.GLOBAL_STORE[cat_id].length <= 0) {
                        // console.log(this.GLOBAL_STORE[cat_id])
                        delete this.GLOBAL_STORE[cat_id];
                    }
                    return true;
                }
            }))
            if (!find) return false;

            this.saveData("carts", this.GLOBAL_STORE);
            return this.GLOBAL_STORE;
        }

        getStorage() {
            if (!this.GLOBAL_STORE) {
                this.queryProduct("carts");
            }
            return this.GLOBAL_STORE;
        }
        queryProduct(attr) {
            var res = localStorage.getItem(attr);

            if (!res || typeof (res) == "undefined" || res == "undefined") {
                res = {};
            }
            else {
                res = JSON.parse(res);

            }
            // res = ( res !==null&& (typeof res!==undefined)) ? JSON.parse(res) : {};

            return res;
        }
        saveData(attr, data) {
            localStorage.setItem(attr, JSON.stringify(data));
        }
        getTotalCount() {
            if (!this.GLOBAL_STORE) return false;
            var count = 0;
            $.each(this.GLOBAL_STORE, (index, item) => {
                item.forEach(element => {
                    count += element.goods_count;
                });

            })
            return count;
        }
    }
    return CarOperate;

});